package com.gxuwz.leaveSystem.dao;

import com.gxuwz.leaveSystem.entity.Classes;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class ClassesDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    // 添加班级
    /**
     * 添加
     * @param classes
     * @return
     */
    public int add(Classes classes) {
        String sql = "INSERT INTO sys_classes (classID, className, depID, major, grade) VALUES (?, ?, ?, ?, ?)";
        Object[] object =new Object[]{
                classes.getClassID(),
                classes.getClassName(),
                classes.getDepID(),
                classes.getMajor(),
                classes.getGrade()
        };
        int num = this.jdbcTemplate.update(sql,object);
        return num;
    }


    /**
     * 删除
     * @param id
     * @return
     */
    // 删除班级
    public int delete(String id) {
        String sql = "DELETE FROM sys_classes WHERE classID = ?";
        int num = this.jdbcTemplate.update(sql,id);
        return num;
    }


    /**
     * 修改
     * @param classes
     * @return
     */

    // 更新班级
    public int update(Classes classes) {
        String sql = "UPDATE sys_classes SET className=?, depID=?, major=?, grade=? WHERE classID=?";
        Object[] object =new Object[]{
                classes.getClassName(),
                classes.getDepID(),
                classes.getMajor(),
                classes.getGrade(),
                classes.getClassID()
        };
        int num = this.jdbcTemplate.update(sql,object);
        return num;
    }


    /**
     * 查询
     * @param classID
     * @return
     */
    // 根据班级ID查询班级
    public Classes findByID(String classID) {
        String sql = "SELECT * FROM sys_classes WHERE classID = ?";
        RowMapper<Classes> rowMapper = new BeanPropertyRowMapper<Classes>(Classes.class);
        return  this.jdbcTemplate.queryForObject(sql,rowMapper,classID);
    }

    /**
     * 查询所有班级
     * @return
     */
    // 查询所有班级
    public List<Classes> findAll() {
        String sql = "SELECT * FROM sys_classes";
        RowMapper<Classes> rowMapper = new BeanPropertyRowMapper<Classes>(Classes.class);
        return  this.jdbcTemplate.query(sql,rowMapper);
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return
     */
    public List<Classes> search(String keyword) {
        String sql = "SELECT * FROM sys_classes WHERE classID LIKE ? OR className LIKE ? OR depID LIKE ? OR major LIKE ? OR grade LIKE ?";
        return jdbcTemplate.query(sql, new Object[]{"%" + keyword + "%","%" + keyword + "%","%" + keyword + "%", "%" + keyword + "%", "%" + keyword + "%"}, new BeanPropertyRowMapper<>(Classes.class));
    }


}
